package cl.orangepeople.whyem.portal.service.impl;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cl.orangepeople.whyem.portal.service.whyemService;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

public class whyemServiceImpl implements whyemService {
    private static Logger logger;
    
    static {
	logger = LoggerFactory.getLogger(whyemServiceImpl.class);
    }
    /*
     * (non-Javadoc)
     * 
     * @see
     * cl.orangepeople.whyem.portal.service.whyemService#existsUserInPortal(
     * java.lang.String)
     */
    public boolean existsUserInPortal(String clientID) {
	logger.debug("Sarching user in portal, client ID: {}", clientID);
	boolean existsUser = false;
	try {
	    int userCount = UserLocalServiceUtil.getUsersCount();
	    List<User> users = UserLocalServiceUtil.getUsers(0, userCount);
	    
	    for (User user : users) { 
		String screenName = user.getScreenName(); // Corresponde a RUT del cliente
		
		logger.debug("User email: {}, screenName: {}", new Object[] { user.getEmailAddress(), screenName });
		if (ObjectUtils.notEqual(screenName, null)) {
		    String userClientID = screenName.toString();
		    boolean clientIDEquals = StringUtils.equals(userClientID, clientID);
		    if (clientIDEquals) {
			existsUser = true;
			break;
		    }
		}
	    }
	} catch (SystemException e) {
	    logger.error("Error en busqueda", e);
	}
	
	return existsUser;
    }
}